/*
 * Server.h
 *
 *  Created on: May 17, 2011
 *      Author: madboyka
 */

#ifndef SERVER_H_
#define SERVER_H_
#include "World.h"
#include <pal.h>
#include "ClientListener.h"
#include "ViewerListener.h"
#include "ClientWorker.h"
#include "ViewerWorker.h"
#include "Console.h"
#include <QVector>
#include <QSettings>
#include <QVariant>

class Console;
class World;
class ViewerWorker;
class ClientWorker;
class ViewerListener;
class ClientListener;

/*!
 * Server class: manager client and viewer listeners and the simulation world.
 */
class Server {
private:
	static Server *instance;
	static Console *console;
	static QSettings *settings;
	World *world;
	ViewerListener *viewerListener;
	ClientListener *clientListener;
	QVector<ViewerWorker*> viewers;
	QVector<ClientWorker*> clients;
	static Console * getConsole();
public:
	Server();
	virtual ~Server();
	void setup();
	/*!
	 * Adds a connected viewer, for the server to serve.
	 */
	void addViewer(ViewerProtocol*);
	/*!
	 * Adds a connected client, for the server to serve.
	 */
	void addClient(ClientProtocol*);
	/*!
	 * Kills the connected viewer and terminated its thread.
	 */
	void killViewer(ViewerWorker*);
	/*!
	 * Kills the connected client and terminated its thread.
	 */
	void killClient(ClientWorker*);
	/*!
	 * Kills the server gracefully.
	 */
	static void kill();
	/*!
	 * Returns the server setting designated by the key, or the default value if not found.
	 */
	static QVariant getSetting(const QString,QVariant);

	friend class ViewerWorker;
	friend class ClientWorker;
	friend class World;
};

#endif /* SERVER_H_ */
